<template>
  <div class="news-detail">
    <div class="container">
      <div v-if="loading" class="loading">
        <div class="loading-spinner"></div>
        <p>加载中...</p>
      </div>

      <div v-else>
        <div class="news-title">
          <h1>{{ info.title || '' }}</h1>
        </div>

        <div class="news-meta">
          <span class="author">作者：{{ info.author || '' }}</span>
          <span class="publish-time">发表时间：{{ info.releaseTime || '' }}</span>
        </div>

        <!-- <div class="news-cover" v-if="info.cover">
          <img :src="publicSrc(info.cover)" :alt="info.title" />
        </div> -->

        <div class="news-content">
          <div class="intro" v-if="info.intro">
            {{ info.intro }}
          </div>
          <div class="details" v-if="info.details">
            <div v-html="info.details"></div>
          </div>
        </div>

        <div class="news-navigation">
          <div class="prev">
            <span class="nav-label">上一篇：</span>
            <router-link
              v-if="info.preNews"
              :to="`/pc/news?id=${info.preNews.id}`"
              class="nav-link"
            >
              {{ info.preNews.title }}
            </router-link>
            <span v-else class="nav-none">暂无上一篇</span>
          </div>
          <div class="next">
            <span class="nav-label">下一篇：</span>
            <router-link
              v-if="info.nextNews"
              :to="`/pc/news?id=${info.nextNews.id}`"
              class="nav-link"
            >
              {{ info.nextNews.title }}
            </router-link>
            <span v-else class="nav-none">暂无下一篇</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
// import { publicSrc } from "@/utils/common";
import { newsInfo } from "@/api/about";
const route = useRoute();
const info = ref<any>({}); // 详情数据
const loading = ref(false); // 加载状态

onMounted(() => {
  getInfo();
});

watch(
  () => route.query.id,
  (newId) => {
    if (newId) {
      getInfo();
    }
  },
  { immediate: false }
);

// 获取新闻详情
const getInfo = () => {
  if (!route.query.id) return;
  loading.value = true;
  info.value = {};
  newsInfo(route.query.id)
    .then((res: any) => {
      if (res.code == 200) {
        info.value = res.data;
      }
    })
    .finally(() => {
      loading.value = false;
    });
};
</script>

<style lang="scss" scoped>
.news-detail {
  min-height: 100vh;
  padding: 200px 20px;
  box-sizing: border-box;
  background: linear-gradient(180deg, #fff 0%, #9bc0e8 100%);
}

.news-title {
  text-align: center;
}

.news-title h1 {
  font-size: 28px;
  font-weight: normal;
  color: #333;
}

.news-meta {
  display: flex;
  justify-content: center;
  gap: 30px;
  color: #666;
  font-size: 12px;
  margin-bottom: 30px;
}

.author,
.publish-time {
  display: flex;
  align-items: center;
}

.news-cover {
  margin-bottom: 30px;
  text-align: center;
  img {
    max-width: 100%;
    height: auto;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }
}

.news-content {
  margin-bottom: 40px;
  line-height: 1.8;
  text-align: left;
}

.intro {
  font-size: 16px;
  color: #555;
  margin: 0 0 20px 0;
  padding: 15px;
  border-radius: 8px;
  background-color: rgba(240, 240, 240, 0.4);
}

.details {
  font-size: 16px;
  color: #333;
}

.details :deep(p) {
  margin: 15px 0;
}

.details :deep(img) {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
  margin: 15px 0;
}

.news-navigation {
  display: flex;
  justify-content: space-between;
  margin-top: 40px;
  padding-top: 40px;
  border-top: 1px solid #e0e0e0;
}

.nav-label {
  color: #666;
  font-size: 14px;
  margin-right: 8px;
}

.nav-link {
  color: #007bff;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s ease;
}

.prev, 
.next {
  max-width: 50vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.nav-link:hover {
  color: #0056b3;
  text-decoration: underline;
}

.nav-none {
  color: #666;
  font-size: 14px;
}

/* 加载状态样式 */
.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  text-align: center;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #007bff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 20px;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.loading p {
  color: #666;
  font-size: 16px;
  margin: 0;
}
</style>
